package xyz.erupt.flow.service.impl;

import lombok.extern.slf4j.Slf4j;
import lombok.var;
import org.springframework.stereotype.Service;
import xyz.erupt.flow.bean.entity.erupt.EruptUserSign;
import xyz.erupt.flow.repository.EruptUserSignRepository;
import xyz.erupt.flow.service.EruptUserSignService;
import xyz.erupt.upms.service.EruptUserService;

import javax.annotation.Resource;

/**
 * @author PP
 * @version 1.0
 * @project Erupt
 * @description
 * @date 2024/9/24 11:58:20
 */
@Slf4j
@Service
public class EruptUserSignServiceImpl implements EruptUserSignService {

    @Resource
    private EruptUserService eruptUserService;

    @Resource
    private EruptUserSignRepository eruptUserSignRepository;
    //签名存在则选择覆盖，不存在则选择储存
    @Override
    public void Save(String sign) {
        var oldSign = eruptUserSignRepository.findByAccount(eruptUserService.getCurrentAccount());

        var signData = new EruptUserSign();
        signData.setSign(sign);
        signData.setAccount(eruptUserService.getCurrentAccount());
        //查找是否已存在数据,存在则替换
        if (oldSign!=null){
            signData.setId(oldSign.getId());
        }
        eruptUserSignRepository.save(signData);
    }

    @Override
    public String GetSign() {
       var account = eruptUserService.getCurrentAccount();
       var data = eruptUserSignRepository.findByAccount(account);
       if(data != null){
           return data.getSign();
       }
       else {
           return null;
       }

    }
}
